import { Component, OnInit } from '@angular/core';
import { MessageService } from 'src/app/service/message.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { User } from 'src/app/model/user';
import { Trainings } from 'src/app/model/trainings';
import { TrainingsDetail } from 'src/app/model/trainingsdetail';

@Component({
  selector: 'app-completedtrainings',
  templateUrl: './completedtrainings.component.html',
  styleUrls: ['./completedtrainings.component.css']
})
export class CompletedtrainingsComponent implements OnInit {
  headers = new HttpHeaders({ 'Content-Type': 'application/json' });

  user: User
  response: any
  list_trainingDetail: TrainingsDetail[] = []
  list_trainings: Trainings[] = []

  isCollapsed = true
  // rate: number
  selectedOptions: any[] = []

  constructor(private http: HttpClient,
    public messageService: MessageService) {
    this.user = JSON.parse(localStorage.getItem('userinfo'))

    if (this.user.type == 3) {
      this.http.get('/api/trainings/findByMentorId/' + this.user.id + '/5')
        .subscribe((data: any) => {
          console.log('/api/trainings/findByMentorId', data)
          this.response = data;
          this.response.forEach(element => {
            this.list_trainingDetail.push(element)
          })
          console.log('this.list_trainingDetail', this.list_trainingDetail)
        })
    }else{
      this.http.get('/api/trainings/findByUserId/' + this.user.id + '/5')
        .subscribe((data: any) => {
          console.log('/api/trainings/findByUserId', data)
          this.response = data;
          this.response.forEach(element => {
            this.list_trainingDetail.push(element)
          })
          console.log('this.list_trainingDetail', this.list_trainingDetail)
        })
    }
  }

  ngOnInit(): void {
  }

  confirmSelection(trainings: Trainings) {
    console.log(trainings)
    this.http.put<any>('/api/trainings/update',
      trainings,
      { headers: this.headers, reportProgress: true })
      .subscribe((data: any) => {
        console.log('/api/trainings/update', data)
        if (data.status == 'success') {

        } else {

        }
      });
  }
  mark(trainings: Trainings) {
    console.log(trainings)
    if (trainings.progress == 100) {
      trainings.trainingsStatus = 5
    }
    this.http.put<any>('/api/trainings/update',
      trainings,
      { headers: this.headers, reportProgress: true })
      .subscribe((data: any) => {
        console.log('/api/trainings/update', data)
        if (data.status == 'success') {
          window.location.assign('/currenttrainings')
        } else {

        }
      });
  }

}
